package com.ubtechinc.cruzr.launcher2.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ubtechinc.cruzr.launcher2.R;
import com.ubtechinc.cruzr.launcher2.model.AppManger;
import com.ubtechinc.cruzr.launcher2.model.entity.AppInfo;
import com.ubtechinc.cruzr.launcher2.presenter.AppPresenter;
import com.ubtechinc.cruzr.launcher2.utlis.LogUtils;
import com.ubtechinc.cruzr.launcher2.utlis.UbtCantans;
import com.ubtechinc.cruzr.launcher2.widget.IclineRelaiveLayout;

import java.util.ArrayList;
import java.util.List;

public class NestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnLongClickListener {
    private Context mContext;
    private int mPowerIndex;
    private int mClockIndex;
    private List<AppInfo> mData;
    private AppPresenter mPresenter;
    private int ItemColor[] = {R.mipmap.third_app0, R.mipmap.third_app1, R.mipmap.third_app2, R.mipmap.third_app3, R.mipmap.third_app4};
    private static final int PAGE_COUNT = 8;
    private static final int Type_Clock = 1000;
    private static final int Type_Power = 1001;
    private static final int Type_Item = 1002;


    public NestAdapter(Context context, AppPresenter presenter) {
        mContext = context;
        mPresenter = presenter;
        mData = new ArrayList<>();
        mData = presenter.getData();
    }

    public void updateAdapter(AppPresenter presenter) {
        mData = presenter.getData();
        notifyDataSetChanged();
    }

    public void setList(AppPresenter presenter) {
        mData = presenter.getData();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == Type_Clock) {
            return new ClockViewHolder(View.inflate(mContext, R.layout.item_clock, null));
        } else if (viewType == Type_Power) {
            return new ContentViewHolder(View.inflate(mContext, R.layout.item_power, null));
        } else {
            return new ContentViewHolder(View.inflate(mContext, R.layout.action_item, null));
//            return new ContentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.action_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final int adapterPosition = holder.getAdapterPosition();
        final AppInfo info = mData.get(position);
        IclineRelaiveLayout icview = (IclineRelaiveLayout) holder.itemView;
        Log.i("NestAdapter", "app name = "+info.appname+"  position = " + position);
//        if (info.appname.equals("广告")) {
//            icview.setBackgroundColor(ContextCompat.getColor(mContext, R.color.item_position_one));
//        }
        holder.itemView.setOnLongClickListener(this);
        if (holder instanceof ClockViewHolder) {
            icview.setClockItem(true);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        } else if (holder instanceof ContentViewHolder) {
            ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
            icview.setClockItem(false);
            int itemViewType = getItemViewType(adapterPosition);
            if (itemViewType == Type_Power) {
                contentViewHolder.setText(R.id.appname, info.appname);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        doAction(info);
                    }
                });
            } else if (itemViewType == Type_Item) {
                contentViewHolder.setText(R.id.appname, info.appname);
                contentViewHolder.setBackgroundResource(R.id.appbg, position, info);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        doAction(info);
                    }
                });
            }
        }
        icview.setThirdApp(!info.isSystemApp);
        icview.setAppName(info.appname);
        if (AppManger.get().isAppDeleteState()) {
            if (info.isSystemApp) {
                icview.mDeleteBtn.setVisibility(View.INVISIBLE);
            } else {
                icview.mDeleteBtn.setVisibility(View.VISIBLE);
                icview.mDeleteBtn.bringToFront();
                icview.mDeleteBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        LogUtils.e("item被点击了");
                    }
                });
            }
            LogUtils.e("当前item = " + info.appname);
            icview.startDeleteAinimation();

        } else {
            icview.mDeleteBtn.setVisibility(View.INVISIBLE);
            icview.stopDeleteAinimation();
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        AppInfo info = mData.get(position);
        if (info.isclock) {
            mClockIndex = position;
            return Type_Clock;
        } else if (info.ispower) {
            mPowerIndex = position;
            return Type_Power;
        } else {
            return Type_Item;
        }
    }

    @Override
    public boolean onLongClick(View view) {
        mPresenter.enterDeleteState();
        notifyDataSetChanged();
        return true;
    }

    private class ContentViewHolder extends RecyclerView.ViewHolder {
        private View itemView;

        ContentViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public ContentViewHolder setText(int id, int text) {
            ((TextView) this.itemView.findViewById(id)).setText(text);
            return this;
        }

        public ContentViewHolder setText(int id, String text) {
            ((TextView) this.itemView.findViewById(id)).setText(text);
            return this;
        }

        public ContentViewHolder setVisible(int id, int visible) {
            this.itemView.findViewById(id).setVisibility(visible);
            return this;
        }

        public ContentViewHolder setOnCliclistener(int id, View.OnClickListener listener) {
            this.itemView.findViewById(id).setOnClickListener(listener);
            return this;
        }

        private ContentViewHolder setBackgroundResource(int viewid, int position, AppInfo info) {
            int id = getIconId(mContext, info.iconname);
            ImageView thirdApp = (ImageView) this.itemView.findViewById(R.id.third_app_icon);
            thirdApp.setVisibility(((id == -1) ? View.VISIBLE : View.GONE));
            ImageView v = (ImageView) this.itemView.findViewById(viewid);
            if (id != -1) {
                v.setBackgroundResource(id);
            } else {
                thirdApp.setImageDrawable(info.res.loadIcon(mContext.getPackageManager()));
                if (info.thirdAppIndex != -1) {
                    derangedView(v, info.thirdAppIndex);
                } else {
                    v.setBackgroundResource(R.mipmap.third_appbg1);
                }
            }
            return this;
        }

        private void derangedView(ImageView v, int position) {
            int index = position % ItemColor.length;
            v.setBackgroundResource(ItemColor[index]);
        }

        int getIconId(Context context, String name) {
            if (TextUtils.isEmpty(name)) {
                return -1;
            }
            int id = context.getResources().getIdentifier(name, "mipmap", context.getPackageName());
            if (id == 0) {
                return -1;
            }
            return id;
        }
    }

    private class ClockViewHolder extends RecyclerView.ViewHolder {
        private View itemView;

        public ClockViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public View getView(int id) {
            return itemView.findViewById(id);
        }

        public ClockViewHolder setText(int id, int text) {
            ((TextView) this.itemView.findViewById(id)).setText(text);
            return this;
        }

        public ClockViewHolder setText(int id, String text) {
            ((TextView) this.itemView.findViewById(id)).setText(text);
            return this;
        }

        public ClockViewHolder setVisible(int id, int visible) {
            this.itemView.findViewById(id).setVisibility(visible);
            return this;
        }

    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    protected void doAction(AppInfo info) {
        LogUtils.e("rqh do action position = " + info.activityname);
        if (info != null) {
            AppManger.get().openActivity(info.packagename, info.activityname, !info.isSystemApp());
        }
    }

    public int getPowerIndex() {
        return mPowerIndex;
    }

    public int getClockIndex() {
        return mClockIndex;
    }

    public int getPageCount() {
        int count;
        if (UbtCantans.CLOCK_VISIBLE) {
            count = mData.size() + 1;
        } else {
            count = mData.size();
        }
        return count / PAGE_COUNT + (count % PAGE_COUNT == 0 ? 0 : 1);
    }

}
